# 4.4 Clear

Interceptors are divided into four levels from top to bottom: Global, Routes, Class, and Method. Clear is used to remove interceptors from the level where it is located and all higher levels.

When Clear is declared at the Method level, it will clear the interceptors at the Global, Routes, and Class levels. When declared at the Class level, it will clear the interceptors at the Global and Routes levels. When the Clear annotation carries parameters, it clears the specified interceptors in the target level.

Tips to remember Clear usage:

  • There are four levels of interceptors: Global, Routes, Class, and Method.
  • Clearing only targets all higher levels from where Clear is declared, without affecting the current and lower levels.
  • When no parameters are provided, all interceptors are cleared. When parameters are present, the specified interceptors are cleared.

In certain application scenarios, it's necessary to remove Global or Class interceptors. For instance, in a backend management system that has a global authorization interceptor, the login action must clear it to complete the login process. Here's a code example:

// The login method needs to remove this authorization interceptor to log in properly.
@Before(AuthInterceptor.class)
public class UserController extends Controller {
    // AuthInterceptor has been cleared by Clear, so it won't be intercepted.
    @Clear
    public void login() {
    }
    
    // This method will be intercepted by AuthInterceptor.
    public void show() {
    }
}
1
2
3
4
5
6
7
8
9
10
11
12

When the Clear annotation has parameters, it can clear specified interceptors. Below is a more comprehensive example:

@Before(AAA.class)
public class UserController extends Controller {
  @Clear
  @Before(BBB.class)
  public void login() {
     // Global and Class level interceptors will be cleared, but BBB declared on this method remains unaffected.
  }
 
  @Clear({AAA.class, CCC.class})// Clear specified interceptors AAA and CCC.
  @Before(CCC.class)
  public void show() {
     // Although CCC is specified to be cleared in the Clear annotation, it cannot be cleared because the clear operation targets only the higher levels.
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

The above clear operations were used on methods, but they can also be applied to classes. For example:

@Clear(AAA.class)
public class UserController {
   public void index() {
      ...
   }
}
1
2
3
4
5
6

As shown above, @Clear(AAA.class) will clear the AAA.java interceptor that's configured in the higher layers, namely Global and Route.

Last Updated: 9/17/2023, 6:05:19 AM